/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedbean;

import entity.ContactEntity;
import entity.SystemUserEntity;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.event.ActionEvent;
import session.stateless.SystemUserSessionBean;
import org.jasypt.util.password.BasicPasswordEncryptor;
import type.UserTitle;

/**
 * change demo
 * @author wei
 */
@ManagedBean(name = "userManagerBean")
@RequestScoped
public class UserManagerBean {

    @EJB
    private SystemUserSessionBean userSessionBean;
    private String name;
    private String password;
    
    private String firstName;
    private String lastName;
    private UserTitle title;
    
    private String email;
    private String address;
    private String contactNumber;
    
    private String userNameLength;
    private String statusMessage;
    private Long userId;

    /** Creates a new instance of UserManagerBean */
    public UserManagerBean() {
        userNameLength = "Current event name length is less than 4.";
    }

    public void saveSystemUser(ActionEvent event) {
        try {
            ContactEntity contact = new ContactEntity();
            password = encryptPassword(password);
            SystemUserEntity user = new SystemUserEntity(name, password, firstName, lastName, title);
            user.setContact(contact);
            userId = userSessionBean.createOrUpdateSystemUser(user).getId();
            statusMessage = "New User Saved Successfully";
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void doAjaxCountUserNameLength() {
        if (name != null) {
            userNameLength = Integer.toString(name.length());
        } else {
            userNameLength = "0";
        }
        userNameLength = "Current user name length is " + userNameLength;
    }

    public String getUserNameLength() {
        return userNameLength;
    }

    public void setUserNameLength(String userNameLength) {
        this.userNameLength = userNameLength;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getStatusMessage() {
        return statusMessage;
    }

    public void setStatusMessage(String statusMessage) {
        this.statusMessage = statusMessage;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getContactNumber() {
        return contactNumber;
    }

    public void setContactNumber(String contactNumber) {
        this.contactNumber = contactNumber;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public UserTitle getTitle() {
        return title;
    }

    public void setTitle(UserTitle title) {
        this.title = title;
    }
    
    private String encryptPassword(String password){
        BasicPasswordEncryptor passwordEncryptor = new BasicPasswordEncryptor();
        String encryptedPassword = passwordEncryptor.encryptPassword(password);
        
        return encryptedPassword;
    }
}
